 // pages/detail/firm/firm.js
let app = getApp();
let testAddr = app.globalData.testAddr;
let imgurl = app.globalData.imgurl;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    login: [{
        id: "0",
        logname: "我的公司"
      },
      {
        id: "1",
        logname: "产品与服务"
      }
    ],
    currentTabsIndex: 0,
    pageIndex: 1,
    allProduct: [],
    show: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var companyID = options.id
    var that = this
    that.setData({
      companyID: companyID,
      imgurl: imgurl
    })
    that.getcominfo(companyID)
    // that.getcomphaddr()
    // that.gethoner()
    that.getproduct(companyID)
  },
  // 获取公司信息
  getcominfo: function(companyID) {
    var that = this;
    app.util.request({
      url: testAddr + "company/searchCompany?companyID=" + companyID,
      method: "POST",
      data: {
        companyID: companyID
      },
      success: function(res) {
        if (res.data.errorCode == 0 && res.data.errorCode != undefined) {
          var jsonStr = res.data.result
          if (typeof jsonStr != 'object' && jsonStr != '') {
            jsonStr = jsonStr.replace(/\ufeff/g, ""); //重点
            var jsonStr = JSON.parse(jsonStr);
          }
          // console.log(jsonStr.merchantID)
          if (jsonStr.merchantID && jsonStr.merchantID != undefined) {
            that.getcomphaddr(jsonStr.merchantID)
            that.gethoner(jsonStr.merchantID)
          }
          that.setData({
            jsonStr: jsonStr
          })
        }
        // console.log(jsonStr)
      }
    })
  },
  // 获取公司地址电话 
  getcomphaddr: function(id) {
    var that = this;
    var id = id
    app.util.request({
      url: testAddr + "merchant/getMerchantInfo?id=" + id,
      method: "POST",
      data: {},
      success: function(res) {
        if (res.data.errorCode == 0 && res.data.errorCode != undefined) {
          var companyaddr = res.data.result
          if (typeof companyaddr != 'object' && companyaddr != '') {
            companyaddr = companyaddr.replace(/\ufeff/g, ""); //重点
            var companyaddr = JSON.parse(companyaddr);
          }
          if (companyaddr.bizNo) {
            companyaddr.bizNo = imgurl + 'bizNo/' + companyaddr.bizNo
          }
          if (companyaddr.companyWork) {
            companyaddr.companyWork = imgurl + 'bizNo/' + companyaddr.companyWork
          }
          that.setData({
            companyaddr: companyaddr
          })
          // console.log(companyaddr)
        }
      }
    })
  },
  // 获取荣誉证书
  gethoner: function(id) {
    var that = this
    var merchantID = id
    app.util.request({
      url: testAddr + "honor/getHonor?merchantID=" + merchantID,
      method: "POST",
      data: {

      },
      success: function(res) {
        if (res.data.errorCode == 0 && res.data.errorCode != undefined) {
          var honerphoto = res.data.result
          if (typeof honerphoto != 'object' && honerphoto != '') {
            honerphoto = honerphoto.replace(/\ufeff/g, ""); //重点
            var honerphoto = JSON.parse(honerphoto);
          }

          for (var i = 0; i < honerphoto.length; i++) {
            if (honerphoto[i].imgUrl) {
              honerphoto[i].imgUrl = imgurl + 'honor/' + honerphoto[i].imgUrl
            }
          }
          that.setData({
            honerphoto: honerphoto
          })
        }
      }
    })
  },
  // 获取产品与服务列表
  getproduct: function(companyID) {
    // console.log(companyID)
    var that = this
    var pageIndex = that.data.pageIndex
    var allProduct = that.data.allProduct
    app.util.request({
      url: testAddr + "Product/getProduct?companyid=" + companyID + "&checkStatus=" + 1 + "&pageIndex=" + pageIndex,
      method: "POST",
      data: {

      },
      success: function(res) {
        if (res.data.errorCode == 0 && res.data.errorCode != undefined) {
          var productlist = res.data.result

          if (typeof productlist != 'object' && productlist != '') {
            productlist = productlist.replace(/\ufeff/g, ""); //重点
            var productlist = JSON.parse(productlist);
          }
          // console.log(productlist)
          for (var j = 0; j < productlist.length; j++) {
            if (productlist[j].productimgs) {
              // console.log(productlist[j].productimgs)
              for (var i = 0; i < productlist[j].productimgs.length; i++) {
                productlist[j].productimgs[i].imgurl = imgurl + 'product/' + productlist[j].productimgs[i].imgurl
              }
            }
          }
          that.setData({
            productlist: productlist
          })
          if (productlist.length != 0) {
            that.setData({
              pageIndex: pageIndex + 1
            })
            allProduct = allProduct.concat(productlist)
          }
        }
        that.setData({
          allProduct: allProduct
        })
        // console.log(allProduct)
      }
    })
  },
  // 进入产品详情
  productdetail: function(e) {
    var that = this;
    var productid = e.currentTarget.dataset.productid
    wx.navigateTo({
      url: '../productdetail/productdetail?productid=' + productid,
    })
    // console.log(productid)
  },
  // 
  onTabsItemTap: function(event) {
    var index = event.currentTarget.dataset.index;
    this.setData({
      currentTabsIndex: index
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this
    var companyID = that.data.companyID
    that.getproduct(companyID)

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})